package com.zut.chat.entity;

import com.mybatisflex.annotation.Id;
import com.mybatisflex.annotation.KeyType;
import com.mybatisflex.annotation.Table;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * 聊天机器人配置实体类
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Table("chat_bot_config")
public class ChatBotConfig {
    
    @Id(keyType = KeyType.Auto)
    private Long id;
    
    /**
     * 机器人名称
     */
    private String botName;
    
    /**
     * 机器人头像URL
     */
    private String botAvatar;
    
    /**
     * 是否启用
     */
    private Boolean enabled;
    
    /**
     * 是否启用自动回复
     */
    private Boolean autoReplyEnabled;
    
    /**
     * 回复概率(0.00-1.00)
     */
    private BigDecimal replyProbability;
    
    /**
     * 最小回复间隔(毫秒)
     */
    private Long minReplyInterval;
    
    /**
     * 最大回复间隔(毫秒)
     */
    private Long maxReplyInterval;
    
    /**
     * 触发关键词，用逗号分隔
     */
    private String triggerKeywords;
    
    /**
     * 回复消息内容，用换行符分隔
     */
    private String replyMessages;
    
    /**
     * 创建时间
     */
    private LocalDateTime createTime;
    
    /**
     * 更新时间
     */
    private LocalDateTime updateTime;
    
    /**
     * 获取触发关键词列表
     */
    public List<String> getTriggerKeywordList() {
        if (triggerKeywords == null || triggerKeywords.trim().isEmpty()) {
            return Arrays.asList();
        }
        return Arrays.asList(triggerKeywords.split(","));
    }
    
    /**
     * 获取回复消息列表
     */
    public List<String> getReplyMessageList() {
        if (replyMessages == null || replyMessages.trim().isEmpty()) {
            return Arrays.asList();
        }
        return Arrays.asList(replyMessages.split("\n"));
    }
    
    /**
     * 检查是否应该触发自动回复
     */
    public boolean shouldTriggerReply(String messageContent) {
        if (!enabled || !autoReplyEnabled || messageContent == null) {
            return false;
        }
        
        List<String> keywords = getTriggerKeywordList();
        if (keywords.isEmpty()) {
            return false;
        }
        
        String content = messageContent.toLowerCase();
        return keywords.stream().anyMatch(keyword -> 
            content.contains(keyword.toLowerCase().trim()));
    }
} 